//
//  SYMobileView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/15.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYMobileView.h"
#import "SYMobileHeaderView.h"
#import "SYMobileTableViewCell.h"
@interface SYMobileView () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYMobileView *mobileView;
@property (strong, nonatomic) UIView *contBgView;
@property (strong, nonatomic) UIView *bgView;//
@property (strong, nonatomic) UILabel *titleLab;
@property (strong, nonatomic) UIButton *quxiaoBtn;
@property (strong, nonatomic) UIButton *quedingBtn;
@property (strong, nonatomic) UILabel *ploLab;
@property (strong, nonatomic) SYCollectionModel *model;//数据源
@property (strong, nonatomic) UITableView *curTableView;//表

@property (strong, nonatomic) NSMutableArray *dataListArr;

@property (copy, nonatomic) NSString *ydId;//需要移动的文件夹id


@end


@implementation SYMobileView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    //背景view
    [self addSubview:self.contBgView];
    [self.contBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    //容器view
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX).offset(60);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_offset(354);
        make.height.mas_offset(250);
    }];
    //标题
    [self.bgView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.top.equalTo(self.bgView.mas_top).offset(15);
    }];
    //取消
    [self.bgView addSubview:self.quxiaoBtn];
    [self.quxiaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(25);
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(40);
    }];
    //确定
    [self.bgView addSubview:self.quedingBtn];
    [self.quedingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(25);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(40);
    }];
    //默认提示
    [self.bgView addSubview:self.ploLab];
    [self.ploLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.top.equalTo(self.bgView.mas_top).offset(47);
    }];
    //表
    [self.bgView addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(75);
        make.left.right.bottom.equalTo(self.bgView);
    }];
}
- (void)showAler:(SYMobileView *)mobileView collectinModel:(SYCollectionModel *)model dataListArr:(NSMutableArray *)dataListArr {
    _model = model;
    _mobileView = mobileView;
    _dataListArr = dataListArr;
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    [window addSubview:mobileView];
    
    [UIView animateWithDuration:0.25 animations:^{
        mobileView.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
        
    }];
    
    _titleLab.text = model.titleName;
    
    [self.curTableView reloadData];
}

#pragma mark - tabel
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //TOP
    return self.dataListArr.count;
//    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYMobileTableViewCell *mobileCell = [tableView dequeueReusableCellWithIdentifier:@"SYMobileTableViewCell" forIndexPath:indexPath];
    SYCollectionModel *collectionModel = self.dataListArr[indexPath.row];
    mobileCell.headerLab.text = collectionModel.titleName;
    
    return mobileCell;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SYMobileHeaderView *headerView = [[SYMobileHeaderView alloc] initWithFrame:CGRectMake(0, 0, 44, 354)];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.headerLab.text = _model.zongFileName;
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SYCollectionModel *collectionModel = self.dataListArr[indexPath.row];
    if ([collectionModel.zyid isEqualToString:self.model.zyid]) {
        [MBProgressHUD zr_showError:@"不能选择自己的文件夹"];
        return;
    }
    
    _ydId = collectionModel.zyid;
    
}


#pragma mark - btnClick
//取消
- (void)quxiaoBtnClick {
    [_mobileView removeFromSuperview];
}
//确定
- (void)quedingBtnClick {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    NSString *onlySort = nil;
    if ([_model.type isEqualToString:@"1"]) {
        onlySort = @"1";
    } else {
        onlySort = @"0";
    }
    NSString *ziyuanid = self.model.zyid;
    
    
    [SYRequestAPI zy_requestMyResourceServiceMoveFileId:ziyuanid typePid:_ydId onlySort:onlySort success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:@"移动成功"];
        
        [NSObject executeRunloop:^{
            
            if (weakSelf.block) {
                weakSelf.block();
            }
            [weakSelf.mobileView removeFromSuperview];
        } afterDelay:1];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
    [_mobileView removeFromSuperview];
}
#pragma mark - set
- (UIView *)contBgView {
    if (!_contBgView) {
        _contBgView = [[UIView alloc] init];
        _contBgView.backgroundColor = [UIColor blackColor];
        _contBgView.backgroundColor = TITLECOLOR;
        _contBgView.alpha = 0.3;
    }
    return _contBgView;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 10;
    }
    return _bgView;
}
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel titleColour:TITLECOLOR
                              titleFount:16
                                pingfang:@"PingFang-SC-Medium"
                               alignment:NSTextAlignmentCenter
                               titleLine:1];
        _titleLab.text = @"新概念L60备课课件.pptx";
    }
    return _titleLab;
}
- (UIButton *)quxiaoBtn {
    if (!_quxiaoBtn) {
        _quxiaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quxiaoBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
        [_quxiaoBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_quxiaoBtn addTarget:self action:@selector(quxiaoBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _quxiaoBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _quxiaoBtn;
}
- (UIButton *)quedingBtn {
    if (!_quedingBtn) {
        _quedingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quedingBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
        [_quedingBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_quedingBtn addTarget:self action:@selector(quedingBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _quedingBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _quedingBtn;
}
- (UILabel *)ploLab {
    if (!_ploLab) {
        _ploLab = [UILabel titleColour:[UIColor colorWithHexString:@"#999999"]
                              titleFount:12
                                pingfang:@"PingFang-SC-Medium"
                               alignment:NSTextAlignmentCenter
                               titleLine:1];
        _ploLab.text = @"将此项目移动到新位置";
//        _ploLab.backgroundColor = [UIColor yellowColor];
    }
    return _ploLab;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_curTableView registerClass:[SYMobileTableViewCell class] forCellReuseIdentifier:@"SYMobileTableViewCell"];
        
    }
    return _curTableView;
}

@end
